/*
 * @Author: szx
 * @Date: 2022-03-09 15:03:36
 * @LastEditTime: 2022-03-09 15:13:59
 * @Description:
 * @FilePath: \leetcode\1000-1099\1051\1051.js
 */
/**
 * @param {number[]} heights
 * @return {number}
 */
var heightChecker = function (heights) {
    const expected = Array.from(heights);
    expected.sort((a, b) => a - b);
    let res = 0;
    for (let i = 0; i < expected.length; i++) {
        if (heights[i] != expected[i]) res++;
    }
    return res;
};

var heightChecker = function (heights) {
    const cnt = new Array(101).fill(0);
    for (let h of heights) cnt[h]++;
    let res = 0;
    let j = 0;
    for (let i = 1; i < cnt.length; i++) {
        while (cnt[i]-- > 0) {
            if (heights[j++] != i) res++;
        }
    }
    return res;
};
